﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace drcoderz.com.gauges.simple
{
    public enum GaugeTheme
    {
        BlueTheme,
        TempTheme,
        CustomTheme
    }

    public class SimpleGauge : Control
    {

        public GaugeTheme GaugeTheme
        {
            get { return (GaugeTheme)GetValue(GaugeThemeProperty); }
            set { 
                SetValue(GaugeThemeProperty, value);

                if (blueTheme != null && tempTheme != null)
                {
                    switch (value)
                    {
                        case GaugeTheme.BlueTheme:
                            blueTheme.Visibility = Visibility.Visible;
                            tempTheme.Visibility = Visibility.Collapsed;
                            customTheme.Visibility = Visibility.Collapsed;
                            break;
                        case GaugeTheme.TempTheme:
                            blueTheme.Visibility = Visibility.Collapsed;
                            tempTheme.Visibility = Visibility.Visible;
                            customTheme.Visibility = Visibility.Collapsed;
                            break;
                        case GaugeTheme.CustomTheme:
                            blueTheme.Visibility = Visibility.Collapsed;
                            tempTheme.Visibility = Visibility.Collapsed;
                            customTheme.Visibility = Visibility.Visible;
                            break;
                    }
                }
            }
        }

        // Using a DependencyProperty as the backing store for GaugeTheme.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GaugeThemeProperty =
            DependencyProperty.Register("GaugeTheme", typeof(GaugeTheme), typeof(SimpleGauge), new PropertyMetadata(null));

        
        public double GaugeValue
        {
            get { return (double)GetValue(GaugeValueProperty); }
            set {

                var angleCalc = value > 100 ? 100 : (value < 0 ? 0 : value);
                angleCalc = Math.Abs((-150) - (angleCalc * 3)) + 60;

                SetValue(GaugeValueProperty, angleCalc);
                if (gaugeRotation != null)
                    gaugeRotation.Angle = angleCalc; 
                    
            }
        }


        public Brush LinesBrush
        {
            get { return (Brush)GetValue(LinesBrushProperty); }
            set { SetValue(LinesBrushProperty, value); }
        }


        public string GaugeLabel
        {
            get { return (string)GetValue(GaugeLabelProperty); }
            set { SetValue(GaugeLabelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for GaugeLabel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GaugeLabelProperty =
            DependencyProperty.Register("GaugeLabel", typeof(string), typeof(SimpleGauge), new PropertyMetadata(null));


        public Brush CustomColor
        {
            get { return (Brush)GetValue(CustomColorProperty); }
            set { SetValue(CustomColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CustomColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CustomColorProperty =
            DependencyProperty.Register("CustomColor", typeof(Brush), typeof(SimpleGauge), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(100 ,29,159,206)) ));

        
        
        // instance fields
        private RotateTransform gaugeRotation;
        private Grid blueTheme;
        private Grid tempTheme;
        private Grid customTheme;

        public override void OnApplyTemplate()
        {
            gaugeRotation = this.GetTemplateChild("gaugeRotation") as RotateTransform;
            
            
            gaugeRotation.Angle = GaugeValue;
            blueTheme = this.GetTemplateChild("blueTheme") as Grid;
            tempTheme = this.GetTemplateChild("tempTheme") as Grid;
            customTheme = this.GetTemplateChild("customTheme") as Grid;

            // Apply Defaults
            LinesBrush = new SolidColorBrush(Colors.White);
            NumbersBrush = new SolidColorBrush(Colors.White);
            GaugeLabel = "TEMP";

            switch (GaugeTheme)
            {
                case GaugeTheme.BlueTheme:
                    blueTheme.Visibility = Visibility.Visible;
                    tempTheme.Visibility = Visibility.Collapsed;
                    customTheme.Visibility = Visibility.Collapsed;
                    break;
                case GaugeTheme.TempTheme:
                    blueTheme.Visibility = Visibility.Collapsed;
                    tempTheme.Visibility = Visibility.Visible;
                    customTheme.Visibility = Visibility.Collapsed;
                    break;
                case GaugeTheme.CustomTheme:
                    blueTheme.Visibility = Visibility.Collapsed;
                    tempTheme.Visibility = Visibility.Collapsed;
                    customTheme.Visibility = Visibility.Visible;
                    break;
            }

            base.OnApplyTemplate();
        }

        public Brush NumbersBrush
        {
            get { return (Brush)GetValue(NumbersBrushProperty); }
            set { SetValue(NumbersBrushProperty, value); }
        }


        // Using a DependencyProperty as the backing store for NumbersBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NumbersBrushProperty =
            DependencyProperty.Register("NumbersBrush", typeof(Brush), typeof(SimpleGauge), new PropertyMetadata(null));

        

        // Using a DependencyProperty as the backing store for LinesBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LinesBrushProperty =
            DependencyProperty.Register("LinesBrush", typeof(Brush), typeof(SimpleGauge), new PropertyMetadata(null));

        

        // Using a DependencyProperty as the backing store for GaugeValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GaugeValueProperty =
            DependencyProperty.Register("GaugeValue", typeof(double), typeof(SimpleGauge), new PropertyMetadata(null));

        public SimpleGauge()
        {
            this.DefaultStyleKey = typeof(SimpleGauge);
            ApplyTemplate();

            //Defaults
        }
    }
}
